package com.yisu.expressway.manager.core.service.impl;

import com.yisu.expressway.ewf.entity.GoodsArchives;
import com.yisu.expressway.ewf.entity.GoodsSpecification;
import com.yisu.expressway.manager.core.mapper.GoodsSpecificationMapper;
import com.yisu.expressway.manager.core.mapper.SpecificationGoodsMapper;
import com.yisu.expressway.manager.core.service.IGoodsSpecificationService;
import com.yisu.expressway.manager.domain.request.GoodsSpecificationRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by xlt on 2017/4/25.
 */
@Service
public class GoodsSpecificationServiceImpl implements IGoodsSpecificationService {
    @Resource
    private GoodsSpecificationMapper goodsSpecificationMapper;
    @Resource
    private SpecificationGoodsMapper specificationGoodsMapper;

    /**
     * 编辑商品规格信息
     *
     * @param goodsSpecificationRequest 商品规格所需数据
     * @return
     */
    @Override
    public boolean modifyGoodsSpecification(GoodsSpecificationRequest goodsSpecificationRequest) {
        try {
            List<GoodsSpecification> parentModels = goodsSpecificationMapper.getGoodsSpecificationListByLevel(goodsSpecificationRequest);
            if (CollectionUtils.isEmpty(parentModels)) {
                GoodsSpecification parentModel = new GoodsSpecification();
                parentModel.setSpecificationNo(Long.valueOf(0));
                modifyGoodsSpecificationCommon(parentModel, 1, goodsSpecificationRequest);
            } else {
                for (GoodsSpecification goodsSpecification : parentModels) {
                    modifyGoodsSpecificationCommon(goodsSpecification, goodsSpecification.getLevel() + 1, goodsSpecificationRequest);
                }
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 重置商品规格数据
     *
     * @param goodsSpecificationRequest 重置商品规格数据
     * @return 成功/失败
     */
    @Override
    public boolean resetGoodsSpecification(GoodsSpecificationRequest goodsSpecificationRequest) {
        try {
            goodsSpecificationMapper.resetGoodsSpecification(goodsSpecificationRequest);
            specificationGoodsMapper.resetSpecificationGoods(goodsSpecificationRequest);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    protected void modifyGoodsSpecificationCommon(GoodsSpecification parentModel, Integer level, GoodsSpecificationRequest goodsSpecificationRequest) {
        if (!StringUtils.isEmpty(goodsSpecificationRequest.getSpecificationName())) {
            SimpleDateFormat sformat = new SimpleDateFormat("yyyyMMddHHmmsss");
            String[] nameArray = StringUtils.split(goodsSpecificationRequest.getSpecificationName(), ",，");
            for (String item : nameArray) {
                Long primaryKey = Long.valueOf(sformat.format(new Date()));
                GoodsSpecification insertModel = new GoodsSpecification();
                insertModel.setCategoryCode(goodsSpecificationRequest.getCategoryCode());
                insertModel.setGoodsNo(goodsSpecificationRequest.getGoodsNo());
                insertModel.setBelongGroupId(Long.valueOf(0));
                insertModel.setBelongGroupName(goodsSpecificationRequest.getBelongGroupName());
                insertModel.setName(item);
                insertModel.setLevel(level);
                insertModel.setFullName(insertModel.getName());
                insertModel.setNodePath(primaryKey + ",");
                if (!StringUtils.isEmpty(parentModel.getFullName())) {
                    insertModel.setFullName(parentModel.getFullName() + "|" + insertModel.getName());
                    insertModel.setNodePath(parentModel.getNodePath() + primaryKey + ",");
                }
                insertModel.setParentId(parentModel.getSpecificationNo());
                if (goodsSpecificationRequest.getSpecificationNo() != null && goodsSpecificationRequest.getSpecificationNo() != 0) {
                    insertModel.setSpecificationNo(goodsSpecificationRequest.getSpecificationNo());
                } else {
                    insertModel.setSpecificationNo(primaryKey);
                }
                goodsSpecificationMapper.insert(insertModel);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 通过分类ID和商品编号获取商品规格列表
     *
     * @param goodsSpecificationRequest 请求参数
     * @return 列表数据
     */
    @Override
    public List<GoodsSpecification> getGoodsSpecificationListByCategoryCodeAndGoodsNo(GoodsSpecificationRequest goodsSpecificationRequest) {
        return goodsSpecificationMapper.getGoodsSpecificationListByCategoryCodeAndGoodsNo(goodsSpecificationRequest);
    }

    /**
     * 纠正错误数据使用，需要删除的方法
     *
     * @return
     */
    @Override
    public List<GoodsSpecification> tepmGetList() {
        return goodsSpecificationMapper.tepmGetList();
    }

    /**
     * 纠正错误使用，需要删除的方法
     *
     * @param goodsSpecifications
     * @return
     */
    @Override
    public boolean tempUpdateSpecificationName(List<GoodsSpecification> goodsSpecifications) {
        try {
            if (!CollectionUtils.isEmpty(goodsSpecifications)) {
                for (GoodsSpecification item : goodsSpecifications) {
                    goodsSpecificationMapper.tempUpdateSpecificationName(item);
                }
            }
            return true;
        } catch (Exception ex) {
            return false;
        }

    }

}
